package com.service.business.base.controller;

import com.service.business.framework.log.RequestLog;
import com.service.business.member.controller.rep.MemberInfoRep;
import com.service.business.base.controller.req.*;
import com.service.business.member.service.MemberService;
import com.service.business.support.SessionUtil;
import com.service.business.support.VerifyCodeUtil;
import com.service.business.utils.IpAddressUtil;
import com.service.core.rest.BaseController;
import com.service.core.rest.response.BaseResponse;
import com.service.core.rest.response.OneResponse;
import com.service.business.member.controller.req.ResetPassReq;
import com.service.dao.constants.ConfigConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <dl>
 * <dt>platform</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2018</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019-04-03</dd>
 * </dl>
 */
@RestController
@RequestMapping()
@Api(tags = "登录相关")
@Slf4j
public class LoginController extends BaseController {

    @Autowired
    private SessionUtil sessionUtil;
    @Autowired
    private MemberService memberService;
    @Autowired
    private VerifyCodeUtil verifyCodeUtil;

    @ApiOperation("登录接口")
    @RequestLog("登录接口")
    @PostMapping("/open/member-login")
    public OneResponse<MemberInfoRep> loginByEmail(@ApiParam @RequestBody @Validated LoginReq loginReq, HttpServletRequest request) {
        verifyCodeUtil.verifyImageVerifyCode(ConfigConstants.IMAGE_VERIFY_CODE, loginReq.getPicCodeId(), loginReq.getPicCode());
        loginReq.setSessionId(request.getSession().getId());
        return successQueryOneResponse(memberService.doLoginMember(loginReq));
    }

    @ApiOperation("邮箱注册接口")
    @RequestLog("邮箱注册接口")
    @PostMapping("/open/member-register/email")
    public BaseResponse registerByEmail(@ApiParam @RequestBody @Validated RegisterReq req, HttpServletRequest request) {
        req.setRegIp(IpAddressUtil.get(request));
        memberService.doMemberEmailRegister(req);
        return successBaseResponse();
    }

    @ApiOperation("手机号注册接口")
    @RequestLog("手机号注册接口")
    @PostMapping("/open/member-register/phone")
    public BaseResponse registerByPhone(@ApiParam @RequestBody @Validated PhoneRegisterReq req, HttpServletRequest request) {
        req.setRegIp(IpAddressUtil.get(request));
        memberService.doMemberPhoneRegister(req);
        return successBaseResponse();
    }

    @ApiOperation("找回登录密码")
    @RequestLog("找回登录密码")
    @PostMapping("/open/reset-pwd/login")
    public BaseResponse resetLoginPwd(@ApiParam @RequestBody @Validated ResetPassReq req) {
        memberService.resetMemberLoginPwd(req);
        return successQueryBaseResponse();
    }

    @ApiOperation("找回支付密码")
    @RequestLog("找回支付密码")
    @PostMapping("/open/reset-pwd/pay")
    public BaseResponse resetSafetyPwd(@ApiParam @RequestBody @Validated ResetPassReq req) {
        memberService.resetMemberSafetyPwd(req);
        return successQueryBaseResponse();
    }

    @ApiOperation("登出")
    @RequestLog("登出")
    @PostMapping("/logout")
    public BaseResponse logout() {
        sessionUtil.removeSession();
        return successSaveBaseResponse();
    }
}
